#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time   : 2023-11-29 21:17
# @Author : hard work
# @File    : set_html_field.py
# @Description :
"""修改手册中的字段，使其charset=utf-8"""
import os
import shutil


def set_file_field(dirs, endwith,find_field, replace_txt):
    raw_fiel_names = []
    filename_list = []
    content, new_content = b'', b''
    # find_field = b'hello'
    # replace_txt = b'hello world'
    files = [os.path.join(dirs, file) for file in os.listdir(dirs)]
    for file in files:
        if os.path.isfile(file):
            if file.endswith(endwith):
                new_path = os.path.join(dirs, file)
                bak_path = new_path + "_bak"
                # filename_list.append(os.path.join(dirs, file))
                # shutil.copy(new_path, bak_path)
                with open(new_path, 'rb') as f:
                    line = f.read()
                    if find_field in line:
                        new_content = line.replace(find_field, replace_txt)
                    else:
                        content = line

                with open(bak_path, 'wb') as f:
                    f.write(content)
                    f.write(new_content)
                shutil.copy(bak_path,new_path)
                os.remove(bak_path)

                # with open(bak_path, 'wb') as f:
                #     f.write(content)
                #     f.write(new_content)
                #
                # with open(bak_path, 'rb') as f:
                #     c = f.read()
                # with open(new_path, 'wb') as f:
                #     f.write(c)

        elif os.path.isdir(file):
            for filename in os.listdir(file):
                # print(filename)
                if filename.endswith(endwith):
                    # filename_list.append(os.path.join(file, filename))
                    new_path = os.path.join(file, filename)
                    bak_path = new_path + "_bak"
                    # filename_list.append(os.path.join(dirs, file))
                    with open(new_path, 'rb') as f:
                        line = f.read()
                        if find_field in line:
                            new_content = line.replace(find_field, replace_txt)
                        else:
                            content = line

                    with open(bak_path, 'wb') as f:
                        f.write(content)
                        f.write(new_content)
                    shutil.copy(bak_path, new_path)
                    os.remove(bak_path)
                    # with open(bak_path, 'rb') as f:
                    #     c = f.read()
                    # with open(new_path, 'wb') as f:
                    #     f.write(c)

                else:
                    pass
    # else:
    #     print('%s no found or not exits' % dir)
    return filename_list, raw_fiel_names


# def set_file_field(dirs, find_field, replace_txt):
#     file_list, raw_file_name = get_dir_all_filename(dirs, '.html')
#     content, new_content = b'', b''
#
#     for file in file_list:
#         print(file)
#         with open(file, 'rb') as f:
#             line = f.read()
#             if find_field in line:
#                 new_content = line.replace(find_field, replace_txt)
#             else:
#                 content = line
#
#         with open(file + '_bak', 'wb') as f:
#             f.write(content)
#             f.write(new_content)
#
#
# def copy_file():
#     dirs = r'D:\cap'
#     file_list, raw_file_name = get_dir_all_filename(dirs, '_bak')
#     for file in file_list:
#         for raw_file in raw_file_name:
#             os.rename(file, raw_file)


if __name__ == '__main__':
    find_field = b'<meta http-equiv="Content-Type" content="text/html" />'
    replace_txt = b'<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />'
    # find_field = b'hello'
    # replace_txt = b'hello world'
    # dirs = 'E:\http_sevice\kg6524_reg-master'
    dirs = r'D:\cap'
    # set_file_field(dirs,".txt", find_field, replace_txt)
    set_file_field(dirs, ".txt",find_field, replace_txt)
    # copy_file()
